package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonArticleMapper;
import com.yungu.swift.common.model.dto.CommonArticleDto;
import com.yungu.swift.common.model.param.AdminArticleParam;
import com.yungu.swift.common.service.CommonArticleService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.common.config.NacosListener.SYS_CONFIG;
import static com.yungu.swift.common.model.dto.CommonArticleDto.*;


/**
 * CommonArticleServiceImpl
 **/
@Service
@Component
public class CommonArticleServiceImpl extends BaseServiceImpl<CommonArticleDto> implements CommonArticleService {

    @Autowired
    private CommonArticleMapper commonArticleMapper;

    @Override
    protected IMapper<CommonArticleDto> getMapper() {
        return commonArticleMapper;
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminArticleParam articleParam) {
        CommonArticleDto articleDto = new CommonArticleDto();
        BeanUtils.copyProperties(articleParam, articleDto);
        if (StringUtils.isEmpty(articleParam.getUuid())) {
            String articleUuid = StringUtils.buildUUID();
            articleDto.setUuid(articleUuid);
            articleDto.setPageViews(0);
            articleDto.setArticleNo(buildArticleNo());
            articleDto.setArticleUrl(buildArticleUrl(articleUuid));
            articleDto.setCreateBy(articleParam.getUserUuid());
            articleDto.setAppid(articleParam.getAppid());
            return add(articleDto);
        }
        articleDto.setUpdateBy(articleParam.getUserUuid());
        return edit(articleDto);
    }

    private String buildArticleUrl(String uuid) {
        return SYS_CONFIG.getAdminHost() + "/h5/article/" + uuid;
    }

    private String buildArticleNo() {
        //获取当日的数量
        Integer count = commonArticleMapper.getTodayCount();
        return DateUtils.format(new Date(), "yyyyMMdd") + String.format("%04d", count);
    }

    @Override
    public ResponseData<Boolean> visitCount(String uuid) {
        return ResponseData.buildSuccessResponse(commonArticleMapper.visitCount(uuid) > 0);
    }

    /**
     * 获取服务隐私政策
     *
     * @param identity 1乘客 2司机
     * @return
     */
    @Override
    public ResponseData<Map<String,Object>> getPrivacyPolicy(Integer identity){
        List<Integer> params;
        if (CommonConstant.USER_IDENTITY_PASSENGER == identity) {
            params = Lists.newArrayList(PAS_SERVICE, USER_PRIVACY, PAS_RECORDING);
        } else {
            params = Lists.newArrayList(DRI_SERVICE, USER_PRIVACY, DRI_RECORDING);
        }
        List<CommonArticleDto> list = commonArticleMapper.listPolicy(params);
        Map<String, Object> resultMap = new HashMap<>(16);
        for (CommonArticleDto articleDto : list) {
            switch (articleDto.getArticleType()) {
                case PAS_SERVICE:
                case DRI_SERVICE:
                    resultMap.put("serviceAgreementUrl", articleDto.getArticleUrl());
                    break;
                case USER_PRIVACY:
                    resultMap.put("privacyPolicyUrl", articleDto.getArticleUrl());
                    break;
                case PAS_RECORDING:
                case DRI_RECORDING:
                    resultMap.put("recordingPolicyUrl", articleDto.getArticleUrl());
                    break;
                default:
                    break;
            }
        }
        return ResponseData.buildSuccessResponse(resultMap);
    };

}